package cn.iocoder.yudao.module.system.service.datasource;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
import cn.iocoder.yudao.module.system.controller.admin.datasource.vo.DataSourceConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.datasource.vo.DataSourceConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.datasource.SystemDataSourceConfigDO;
import cn.iocoder.yudao.module.system.dal.mysql.datasource.SystemDataSourceConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.DATASOURCE_CONFIG_NOT_EXISTS;

/**
 * 数据源配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service("systemDataSourceConfigService")
@Validated
@Slf4j
public class SystemDataSourceConfigServiceImpl implements DataSourceConfigService {

    @Resource
    @Qualifier("systemDataSourceConfigMapper")
    private SystemDataSourceConfigMapper systemDataSourceConfigMapper;

    @Override
    public Long createDataSourceConfig(DataSourceConfigSaveReqVO createReqVO) {
        // 插入
        SystemDataSourceConfigDO dataSourceConfig = BeanUtils.toBean(createReqVO, SystemDataSourceConfigDO.class);
        systemDataSourceConfigMapper.insert(dataSourceConfig);
        // 返回
        return dataSourceConfig.getId();
    }

    @Override
    public void updateDataSourceConfig(DataSourceConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateDataSourceConfigExists(updateReqVO.getId());
        // 更新
        SystemDataSourceConfigDO updateObj = BeanUtils.toBean(updateReqVO, SystemDataSourceConfigDO.class);
        systemDataSourceConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataSourceConfig(Long id) {
        // 校验存在
        validateDataSourceConfigExists(id);
        // 删除
        systemDataSourceConfigMapper.deleteById(id);
    }

    private void validateDataSourceConfigExists(Long id) {
        if (systemDataSourceConfigMapper.selectById(id) == null) {
            throw exception(DATASOURCE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public SystemDataSourceConfigDO getDataSourceConfig(Long id) {
        return systemDataSourceConfigMapper.selectById(id);
    }

    @Override
    public PageResult<SystemDataSourceConfigDO> getDataSourceConfigPage(DataSourceConfigPageReqVO pageReqVO) {
        return systemDataSourceConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SystemDataSourceConfigDO> getDataSourceConfigList() {
        return systemDataSourceConfigMapper.selectList();
    }

    @Override
    public boolean testConnection(Long id) {
        SystemDataSourceConfigDO config = getDataSourceConfig(id);
        if (config == null) {
            return false;
        }
        
        return JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword());
    }

}